package cc.eevee.jni.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import cc.eevee.jni.libdynamic.Bird;
import cc.eevee.jni.libdynamic.Injector;
import cc.eevee.jni.libdynamic.Math;
import cc.eevee.jni.libstatic.Strings;
import java.util.Locale;

@SuppressWarnings("WeakerAccess")
public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private TextViewHelper mTextView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = findViewById(R.id.text);
    mTextView = new TextViewHelper(textView);

    //String text = Strings.helloFromJNI();
    String text = new Strings().hello("伊布@cc");
    mTextView.append(text);

    mTextView.newline();
    invokeMathMethod("toRadians", 90.);
    invokeMathMethod("toDegrees", Math.PI_2);
    invokeMathMethod("sin", Math.PI_2);
    invokeMathMethod("asin", Math.sin(Math.PI_2));
    invokeMathMethod("cos", Math.PI_2);
    invokeMathMethod("acos", Math.cos(Math.PI_2));
    invokeMathMethod("tan", Math.PI);
    invokeMathMethod("atan", Math.tan(Math.PI));
    invokeMathMethod("atan2", 1., 1.);
    //invokeMathMethod("atan2", 1, 1);

    mTextView.newline();
    mTextView.newline().append(new Bird("dove 鸽").greeting());
    mTextView.newline().append(new Bird("hawk 鹰").greeting());
  }

  private void invokeMathMethod(String name, Object... args) {
    Object result = Injector.invoke(Math.class, name, args);

    String[] argNames = new String[args.length];
    for (int i = 0, n = args.length; i < n; i++) {
      argNames[i] = toString(args[i]);
    }
    String invokeName = String.format("%s(%s)", name, Strings.join(", ", argNames));

    mTextView.newline();
    mTextView.append(invokeName + ": ");
    mTextView.append(toString(result), Color.MAGENTA);

    if (result == null) {
      Log.w(TAG, String.format("invokeMathMethod %s not found", invokeName));
    }
  }

  private String toString(Object obj) {
    if (obj == null) return "null";
    Class<?> clazz = obj.getClass();
    if (clazz.equals(Float.class) || clazz.equals(Double.class)) {
      return String.format(Locale.getDefault(), "%.4f", obj);
    }
    return obj.toString();
  }

  private static class TextViewHelper {

    private TextView mTextView;

    public TextViewHelper(TextView textView) {
      mTextView = textView;
      clear();
    }

    public TextViewHelper append(CharSequence text) {
      return append(text, Color.BLACK);
    }

    public TextViewHelper append(CharSequence text, int color) {
      SpannableString str = new SpannableString(text);
      str.setSpan(new ForegroundColorSpan(color), 0, str.length(),
          Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
      mTextView.append(str);
      return this;
    }

    public TextViewHelper newline() {
      mTextView.append("\n");
      return this;
    }

    public TextViewHelper clear() {
      mTextView.setText("");
      return this;
    }
  }

}
